package com.hbnu.personaldiaryweb.mapper;


import com.hbnu.personaldiaryweb.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

//用户实体查询窗口、
@Mapper
public interface UsersMapper {



    // 插入用户
    @Insert("INSERT INTO users (id, username, password) VALUES (#{id}, #{username}, #{password})")
    int insertUser(User user);



    //根据用户名查找用户，用户名为唯一字段
    @Select("select * from users where username = #{username}")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER),
            @Result(property = "username", column = "username",jdbcType = JdbcType.VARCHAR),
            @Result(property = "password", column = "password",jdbcType = JdbcType.VARCHAR)
    })
    public User getByName(String username);



    // 查询所有用户
    @Select("SELECT id, username, password FROM users")
    @Results({
            @Result(property = "id", column = "id", jdbcType = JdbcType.VARCHAR),
            @Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
            @Result(property = "password", column = "password", jdbcType = JdbcType.VARCHAR)
    })
    List<User> getAllUsers();



    // 更新用户信息
    @Update("UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}")
    int updateUser(User user);



    // 根据 ID 删除用户
    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteUserById(String id);


}
